package com.hpeng.basic.excel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.Font;
import jxl.format.Format;
import jxl.format.Orientation;
import jxl.format.Pattern;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.CategoryManager;
import com.hpeng.sys.model.Category;
import com.opensymphony.xwork2.ActionSupport;

@Component("downloadCategory")
@Scope("prototype")
public class CategoryOutputExcelAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {
	@Resource
	private CategoryManager categoryManager;

	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	
	public void setCategoryManager(CategoryManager categoryManager){
		this.categoryManager = categoryManager;
	}

	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	// 获取输出流
	public InputStream getInputExcel() {
		return (InputStream) request.getAttribute("excelStream");
	}

	@Override
	public String execute() {
		List<Category> list = categoryManager.findCategory();
		
		String[] columnName = {"资产类型编号","资产类型名称","资产类型简称","资产类型别名","资产规格","资产单位","资产最小储量","资产最大储量","资产实际储量"};
		
		if (list != null) {
			int page = 0;
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			WritableWorkbook book;
			try {
				//避免乱码
				WorkbookSettings setting = new WorkbookSettings();
				Locale local = new Locale("zh", "CN");
				setting.setLocale(local);
				setting.setEncoding("ISO-8859-1");
				//设置样式
				book = Workbook.createWorkbook(os,setting);
				
				WritableSheet sheet = book.createSheet("第一页", page);//设置工作簿当前页数
				//设置标题
				int startColumn = columnName.length/2-1;
				int startRow = 0;
				int endColumn = columnName.length/2+1;
				int endRow = 0;
				sheet.mergeCells(startColumn, startRow, endColumn, endRow);
				Label label = new Label(7, 0, "资产类别报表");
				WritableCellFormat wc = new WritableCellFormat();
				wc.setAlignment(Alignment.CENTRE);
				label.setCellFormat(wc);
				sheet.addCell(label);
				//设置表头
				for(int j=0; j<columnName.length; j++){
					//Label..第一个参数为列,第二个参数为行,第三个参数为值.
						Label label1 = new Label(j, 1, columnName[j]);
						sheet.addCell(label1);
				}
				//输出资产类型值
				for (int i = 1; i < list.size(); i++) {
					int row = i+2;
					Label label2 = new Label(0,row,list.get(i).getCatid());
					Label label3 = new Label(1,row,list.get(i).getCatname());
					Label label4 = new Label(2,row,list.get(i).getCatsimplename());
					Label label5 = new Label(3,row,list.get(i).getCatalias());
					Label label6 = new Label(4,row,list.get(i).getCatnorms());
					Label label7 = new Label(5,row,list.get(i).getCatunit());
					Label label8 = new Label(6,row,list.get(i).getCatlowstock()+"");
					Label label9 = new Label(7,row,list.get(i).getCathighstock()+"");
					Label label10 = new Label(8,row,list.get(i).getCatactualstock()+"");
					sheet.addCell(label2);
					sheet.addCell(label3);
					sheet.addCell(label4);
					sheet.addCell(label5);
					sheet.addCell(label6);
					sheet.addCell(label7);
					sheet.addCell(label8);
					sheet.addCell(label9);
					sheet.addCell(label10);
				}
				book.write();
				book.close();
				request.setAttribute("excelStream", new ByteArrayInputStream(os
						.toByteArray()));
				request.setAttribute("filename", "myexcel.xls");//设置文件名 
				return "success";
			} catch (IOException e) {
				e.printStackTrace();
				return "error";
			} catch (RowsExceededException e) {
				e.printStackTrace();
				return "error";
			} catch (WriteException e) {
				e.printStackTrace();
				return "error";
			}
		}else{
			System.out.println("统计数据不存在");
			return "error";
		}
	}
}
